package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.Session;
import com.mysql.cj.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author shenrui
 * @date 2021/9/20
 * @description 设置User的行为，包括登录，注册
 */
@Controller
public class UserController {
    @Resource
    UserService userService;

    /**
     *@description 用户登录逻辑
     * @return String
     */
    @PostMapping("/user_login")
    public String userLogin(User user, HttpSession session, Model model) {
        //防止用户卡bug进入登陆页面
        if (session.getAttribute("userLogin") != null) {
            return "redirect:user_main";
        }
        //管理员不能同时登录用户
        if (session.getAttribute("adminLogin") != null) {
            return "redirect:admin_main";
        }
        if (StringUtils.isNullOrEmpty(user.getUsrAccount()) || StringUtils.isNullOrEmpty(user.getUsrPassword())) {
            model.addAttribute("userMsg", "请输入用户名或密码");
            return "user/user_login";

        }
        User myUser = userService.verifyUserLogin(user);
        if (myUser != null) {
            session.setAttribute("userLogin", myUser);
            return "redirect:user_main";
        } else {
            model.addAttribute("userMsg", "用户名或密码错误");
            return "user/user_login";
        }

    }

    /**
     *@description 用户退出登录
     * @return String
     */
    @RequestMapping("/user_logout")
    public String user_logout(HttpSession session) {
        session.invalidate();;
        return "redirect:user_login";
    }

    /**
     *@description 用户的注册
     * @return String
     */
    @PostMapping("/user_register")
    public String userRegister(User user, HttpSession session, Model model) {
        //防止用户卡bug进入注册页面
        if (session.getAttribute("userLogin") != null) {
            return "redirect:user_main";
        }
        //填入信息不能为空
        if (StringUtils.isNullOrEmpty(user.getUsrAccount()) || StringUtils.isNullOrEmpty(user.getUsrPassword()) || StringUtils.isNullOrEmpty(user.getUsrName())) {
            model.addAttribute("userMsg", "信息不能为空");
            return "user/user_register";

        }
        //昵称不能重复
        if (userService.count(new LambdaQueryWrapper<User>().eq(User::getUsrName, user.getUsrName())) > 0) {
            model.addAttribute("userMsg", "昵称重复");
            return "user/user_register";
        }
        //用户名不能重复
        if (userService.count(new LambdaQueryWrapper<User>().eq(User::getUsrAccount, user.getUsrAccount())) > 0) {
            model.addAttribute("userMsg", "用户名重复");
            return "user/user_register";
        }
        //判断是否成功插入数据库
        if(userService.save(user)) {
            return "user/user_login";
        } else {
            model.addAttribute("userMsg", "注册失败");
            return "user/user_register";
        }
    }

    /**
     *@description 获取用户列表
     * @return String
     */
    @GetMapping("/user_list")
    public String userList(HttpSession session) {
        List<User> userList = userService.list();
        session.setAttribute("userList", userList);
        return "admin/user_list";
    }
    /**
     *@description 删除用户
     * @return String
     */
    @GetMapping("/user_delete/{id}")
    public String deleteUser(@PathVariable("id") Long id) {
        userService.removeById(id);
        return "redirect:/user_list";
    }

    /**
     *@description 跳转到修改用户信息的界面
     * @return String
     */
    @GetMapping("/user_revice_jump/{id}")
    public String reviceUserJump(@PathVariable("id") Long id,
                                  HttpSession session) {
        User user = userService.getById(id);
        session.setAttribute("userInfo",user);
        return "redirect:/revice_user_info";
    }

    /**
     *@description 修改用户信息
     * @return String
     */
    @PostMapping("/revice_user_info")
    public String reviceUserInfo(User user, HttpSession session, Model model) {
        //填入信息不能为空
        if (StringUtils.isNullOrEmpty(user.getUsrName()) || StringUtils.isNullOrEmpty(user.getUsrPassword())) {
            model.addAttribute("adminMsg", "信息不能为空");
            return "admin/revice_user_info";

        }
        //昵称不能重复
        if (userService.count(new LambdaQueryWrapper<User>().ne(User::getUsrId, user.getUsrId()).eq(User::getUsrName, user.getUsrName())) > 0) {
            model.addAttribute("adminMsg", "昵称重复");
            return "admin/revice_user_info";
        }
        //进行更新
        Boolean bool = userService.updateById(user);
        if (bool) {
            return "redirect:/user_list";
        } else {
            model.addAttribute("adminMsg", "修改失败");
            return "admin/revice_user_info";
        }

    }
}
